﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using SRB.Tg.Model;
using Teigha.Runtime;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Cad_Bll;
using Teigha.Export_Import;
using System.Drawing;
using Teigha.Geometry;
using Teigha.GraphicsSystem;
using SRB.Tg.Cad_Bll;
using System.Collections.Specialized;
using System.IO;
using SRB.Tg.Operate;
using SRB.Tg.Operate.Operate;
using Teigha.DatabaseServices.Filters;
using SRB.Tg.Common;

namespace ExportCADFiles
{
    public class ExportPierSubstructure
    {
        static string appPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "CADFiles";
        static System.Data.DataTable dtProject;
        static System.Data.DataTable dtProjectConfig;
        
        public static string DrawingPier(string pierParaFile, string catalogid, string _appPath = "", int scale = 15)
        {
            WriteLogs wl = new WriteLogs();
            string filePath = "";
            int index = 0;
            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.Pier, out dtProject);
            string file = "";
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            List<string> fileList = new List<string>();
            //filePath = file.Replace(".dwg", "") + ".pdf";
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index>0) 
            {
                int j = 0;
                for (int i = 0; i < index; i++)
                {
                    bool result = false;
                    using (Services svc = new Services())
                    {
                        wl.writeLogs("加载基本配置", "exelog");
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            wl.writeLogs("绘图规则初始化", "exelog");
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            //更新Extensions

                            wl.writeLogs("开始绘图", "exelog");

                            result = P1.DrawingPierGetOne(db, scale, i, index, pierParaFile, out dtProjectConfig, catalogid,j);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\Pier");
                                file = appPath + "\\Pier\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (j + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);
                            }
                        }
                    }
                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {

                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                        }
                    }
                    if (result)
                    {
                        i++;
                    }
                    j++;
                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";
            return filePath;
        }

        public static string DrawingBeamPierRebar(string pierParaFile, string catalogid, string _appPath = "")
        {
            string filePath = "";
            int index = 0;
            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.BeamRebar,out dtProject);
            string file = "";
            List<string> fileList = new List<string>();
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            //filePath = file.Replace(".dwg", "") + ".pdf";
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index>0) 
            {
                for (int i = 0; i < index; i++)
                {
                    using (Services svc = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            P1.DrawingPierGetOne(db, 8, i, index, pierParaFile, out dtProjectConfig, catalogid);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {

                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\BeamRebar");
                                file = appPath + "\\BeamRebar\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (i + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);
                            }
                        }
                    }

                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {

                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);

                        }
                    }

                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }

        public static string DrawingPierRebar(string pierParaFile, string catalogid, string _appPath = "")
        {
            string filePath = "";
            int index = 0;

            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.PierRebar, out dtProject);
            string file = "";
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            List<string> fileList = new List<string>();
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index > 0) 
            {
                for (int i = 0; i < index; i++)
                {
                    using (Services svc = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            P1.DrawingPierGetOne(db, 8, i, index, pierParaFile, out dtProjectConfig, catalogid);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {

                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\PierRebar");
                                file = appPath + "\\PierRebar\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (i + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);
                            }
                        }
                    }

                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {
                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                        }
                    }
                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }

        public static string DrawingPileCapRebar(string pierParaFile, string catalogid, string _appPath = "")
        {
            string filePath = "";
            int index = 0;
            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.PileCapRebar, out dtProject);
            string file = "";
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            List<string> fileList = new List<string>();
            //filePath = file.Replace(".dwg", "") + ".pdf";
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index > 0 ) 
            {
                for (int i = 0; i < index; i++)
                {
                    using (Services svc = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            P1.DrawingPierGetOne(db, 8, i, index, pierParaFile, out dtProjectConfig, catalogid);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {

                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\PileCapRebar");
                                file = appPath + "\\PileCapRebar\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (i + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);

                            }
                        }
                    }

                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {

                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                        }
                    }

                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;

                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }

        public static string DrawingPileRebar(string pierParaFile, string catalogid, string _appPath = "")
        {
            string filePath = "";
            int index = 0;
            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.PileRebar, out dtProject);
            string file = "";
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            List<string> fileList = new List<string>();
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index>0)
            {
                for (int i = 0; i < index; i++)
                {
                    using (Services svc = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            P1.DrawingPierGetOne(db, 8, i, index, pierParaFile,out dtProjectConfig, catalogid);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {

                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\PileRebar");
                                //drPileRebar = dtProject.Select("name like '%引桥桩基%'" );
                                file = appPath + "\\PileRebar\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (i + 1).ToString() + ".dwg";
                                //file = appPath + "\\PileRebar\\" + drPileRebar[0][3].ToString() + " " + drPileRebar[0][2].ToString() + (i + 1).ToString() + ".dwg";

                                //file = appPath + "\\PileRebar\\" + dtProject.Rows[4][2].ToString() + " " + dtProject.Rows[4][1].ToString() + (i + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);

                            }
                        }
                    }

                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {

                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                        }
                    }

                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                //string fileName = drPileRebar[0][3].ToString() + " " + drPileRebar[0][2].ToString();
                //string fileName = dtProject.Rows[4][2].ToString() + " " + dtProject.Rows[4][1].ToString();
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }

        public static string DrawingAbutmentPileRebar(string pierParaFile, string catalogid,string _appPath = "")
        {
            string filePath = "";
            int index = 0;
            DrawingPierStructure P1 = new DrawingPierStructure(pierParaFile, PierSubstructureType.AbutmentPileRebar, out dtProject);
            string file = "";
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            List<string> fileList = new List<string>();
            P1.GetDrawingPierindex(ref index);
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            if (index > 0)
            {
                for (int i = 0; i < index; i++)
                {
                    using (Services svc = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(svc);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        using (Database db = new Database())
                        {
                            HostApplicationServices.WorkingDatabase = db;

                            //db = new Database();
                            InitConfig.InitStandardConfig(db, pierParaFile);
                            P1.DrawingPierGetOne(db, 8, i, index, pierParaFile, out dtProjectConfig, catalogid);
                            drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                            //LayoutManager LM = LayoutManager.Current;
                            //LM.CurrentLayout = "图纸";
                            if (db != null)
                            {

                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "Model";
                                //CADTools.zoom_extents_handler(db);
                                //LM.CurrentLayout = "图纸";
                                //CADTools.zoom_extents_handler(db);
                                creatFolder(appPath + "\\AbutmentPileRebar");
                                file = appPath + "\\AbutmentPileRebar\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (i + 1).ToString() + ".dwg";
                                db.SaveAs(file, DwgVersion.vAC18);

                            }
                        }
                    }

                    using (Services ser = new Services())
                    {
                        SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                        SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                        HostApplicationServices.Current = new HostAppServ(ser);
                        Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                        //打开数据库(dwg⽂件)
                        using (Database database = new Database(false, false))//不加参数会出错
                        {

                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                            LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                        }
                    }

                }
                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion
            }

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }


        public static void creatFolder(String path)
        {
            //不存在 则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
    }
}
